package ymy;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author com.ymy.hadoop
 * @date 2018/12/15 13:14
 */
public class Reflection {

    @Test
    public void test01()throws Exception{
        Class<?> clazz =  Class.forName("java.lang.Object");
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods){
            System.out.println(method);
        }
        System.out.println("-------------------");
        methods = clazz.getMethods();
        for (Method method :methods){
            System.out.println(method);
        }
    }

    @Test
    public void test02()throws Exception{
       Class<?> clazz = String.class;
       Method[] methods = clazz.getMethods();
        for (Method method:methods) {
            System.out.println(method);
        }
    }

    @Test
    public void test03(){
        System.out.println(Integer.class);
        System.out.println(Integer.TYPE);
    }

    @Test
    public void test04(){
       Class<?> classType = Child.class;
       System.out.println(classType);

       classType = classType.getSuperclass();
       System.out.println(classType);

       classType = classType.getSuperclass();
       System.out.println(classType);

       classType = classType.getSuperclass();
       System.out.println(classType);
    }

    @Test
    public void test05() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // java改变原有方法的参数，传入原有方法不同的参数类型
        List<String> teachers = new ArrayList<String>();
        teachers.add("苍老师");
        teachers.add("泷老师");
//        teachers.add(234);
        Class<?> clazz = teachers.getClass();

        Method method = clazz.getDeclaredMethod("add",Object.class);
        method.invoke(teachers,234);
        method.invoke(teachers,567);
        method.invoke(teachers,"波老师");

        for (Object obj: teachers) {
            System.out.println(obj);
        }
    }
}

class Parent{

}

class Child extends Parent{

}